import sys
import unittest
import coverage

class TestKwonly(unittest.TestCase):
    def test_kwonly(self):
        from kwonly import kwonly

        @kwonly(0)
        def f(a, b, c):
            return [a, b, c]

        with self.assertRaisesRegexp(
            TypeError,
            'f\(\) takes exactly 0 arguments \(3 given\)',
        ):
            f(1, 2, 3)

        self.assertEqual(f(a=1, b=2, c=3), [1, 2, 3])

    def test_kwonly_nodefault(self):
        from kwonly import kwonly, NoDefault

        @kwonly(2)
        def f(a, b=1, c=NoDefault, d=2):
            return [a, b, c, d]

        with self.assertRaisesRegexp(
            TypeError,
            'f needs keyword-only argument c',
        ):
            f(0)

        self.assertEqual(f(0, c=3), [0, 1, 3, 2])

    def test_kwonly_varargs(self):
        from kwonly import kwonly

        @kwonly(0)
        def f(a, *args):
            return [a, args]

        self.assertEqual(f(1, 2, a=0), [0, (1, 2)])

    def test_kwonly_keywords(self):
        from kwonly import kwonly

        @kwonly(0)
        def f(a, **kwargs):
            return [a, kwargs]

        self.assertEqual(f(b=1, c=2, a=0), [0, {'b': 1, 'c': 2}])

    def test_kwonly_string_kwdefault(self):
        from kwonly import kwonly

        @kwonly(0)
        def f(a='foo'):
            return a

        self.assertEqual(f(), 'foo')

    def test_kwonly_string_posdefault(self):
        from kwonly import kwonly

        @kwonly(1)
        def f(a='foo', b='bar'):
            return a, b

        self.assertEqual(f(), ('foo', 'bar'))

if __name__ == '__main__':
    cov = coverage.coverage(branch=True, omit=__file__)
    cov.start()

    try:
        unittest.main()
    finally:
        cov.stop()
        cov.html_report(directory='covhtml')
